package cn.niit.smbms.controller.admin;

import cn.niit.smbms.entity.SysUserReceiveAddres;
import cn.niit.smbms.entity.User;
import cn.niit.smbms.service.SysUserReceiveAddresService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收货地址控制器
 *
 * @author xiaomi
 */
@Slf4j
@RequestMapping("/useraddres")
@Controller
public class SysUserReceiveAddresController {
    @Autowired
    private SysUserReceiveAddresService sysUserReceiveAddresService;

    /**
     * 收货地址列表页面
     * @param model
     * @param addres
     * @return
     */
    @GetMapping("")
    public String queryAddress(Model model, SysUserReceiveAddres addres) {
        List<SysUserReceiveAddres> addressList = sysUserReceiveAddresService.selectByCondition(addres);
        model.addAttribute("addressList", addressList);
        return "useraddres/list";
    }

    /**
     * 进入添加收货地址页面、或者是修改页面
     * @return
     */
    @GetMapping("/{userId}/{id}")
    public String savaAddress(Model model, @PathVariable("userId") Long userId, @PathVariable("id") Long id){
        SysUserReceiveAddres addres;
        if(null != id && id > 0){
            /*TODO 修改页面*/
            addres = sysUserReceiveAddresService.selectByreceiveId(id);
        }else{
            /*TODO 新增页面*/
            addres = new SysUserReceiveAddres();
        }
        addres.setSysUserId(userId);
        model.addAttribute("addres", addres);
        return "useraddres/add";
    }

    /**
     *添加收货地址点击“确认保存”按钮后的请求方法
     * @param addres
     * @return
     */
    @PostMapping("")
    public String addRecruitPlan( @Validated SysUserReceiveAddres addres, Model model, BindingResult result){
        log.info("待添加的收货地址：{}" + addres);
        if (result.hasErrors()){
            Map<String, String> errMap = new HashMap<>();
            for (FieldError error : result.getFieldErrors()) {
                log.warn("前端校验报错信息:", error.getField(), error.getDefaultMessage());
                errMap.put(error.getField(), error.getDefaultMessage());
            }
            model.addAttribute("errors", errMap);
            model.addAttribute("addres", addres);
            return "useraddres/add";
        }
        int rows = sysUserReceiveAddresService.add(addres);
        System.out.println("rows" + rows);
        return "redirect:/useraddres";
    }

    /**
     * 更新收货地址
     * @param addres
     * @param request
     * @return
     */
    @PutMapping("/{id}")
    public String saveAddress(SysUserReceiveAddres addres, HttpServletRequest request){
        addres.setGmtCreate(new Date());
        int rows = sysUserReceiveAddresService.update(addres);
        log.info("被更新的记录数:{}", rows);
        if (rows <= 0){
            return "useraddres/add";
        }
        return "redirect:/useraddres";
    }
}
